package com.book.service.impl;

import com.book.entity.Account;
import com.book.entity.BookInfo;
import com.book.mapper.BookInfoMapper;
import com.book.service.BookInfoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author SuperYu  2024/7/29
 */
@Service
public class BookInfoServiceImpl implements BookInfoService {

    BookInfoMapper bookInfoMapper;

    @Autowired
    public void setBookInfoMapper(BookInfoMapper bookInfoMapper) {
        this.bookInfoMapper = bookInfoMapper;
    }

    @Override
    public List<BookInfo> findAllBookInfo() {
        return bookInfoMapper.selectAll();
    }

    @Override
    public List<BookInfo> findGoodBook() {
        Page<BookInfo> page = PageHelper.startPage(1, 8);
        Example example = new Example(BookInfo.class);
        example.setOrderByClause("book_sale_count desc");
        List<BookInfo> list = bookInfoMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<BookInfo> findNewBook() {
        Page<BookInfo> page = PageHelper.startPage(1, 8);
        Example example = new Example(BookInfo.class);
        example.setOrderByClause("publisher_date desc");
        List<BookInfo> list = bookInfoMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<BookInfo> findCollectionBook() {
        Page<BookInfo> page = PageHelper.startPage(1, 8);
        Example example = new Example(BookInfo.class);
        example.setOrderByClause("book_collection_count desc");
        List<BookInfo> list = bookInfoMapper.selectByExample(example);
        return list;
    }

    @Override
    public PageInfo<BookInfo> findBookInfoByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<BookInfo> list = bookInfoMapper.selectAll();
        return new PageInfo<>(list);
    }

    @Override
    public BookInfo findBookInfoById(int id) {
        return bookInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public int editBookInfo(BookInfo book) {
        return bookInfoMapper.updateByPrimaryKeySelective(book);
    }

    @Override
    public BookInfo findBookInfoByName(String name) {
        Example example = new Example(BookInfo.class);
        // 创建查询规则
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("bookName", name);

        return bookInfoMapper.selectOneByExample(example);
    }

    @Override
    public List<BookInfo> findBookInfoLikeName(String name) {
        Example example = new Example(BookInfo.class);
        // 创建查询规则
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("bookName", "%" + name + "%");

        return bookInfoMapper.selectByExample(example);
    }

    @Override
    public PageInfo<BookInfo> findBookInfoByCondition(BookInfo bookInfo, double endPrice, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        Example example = new Example(BookInfo.class);
        Example.Criteria c = example.createCriteria();
        // 根据名字查
        if (bookInfo.getBookName() != null && bookInfo.getBookName().length() != 0) {
            c.andLike("bookName", "%" + bookInfo.getBookName() + "%");
        }
        // 根据目录查
        if (bookInfo.getTypeId() != null && bookInfo.getTypeId() != 0) {
            c.andEqualTo("typeId", bookInfo.getTypeId());
        }
        // 根据价格范围查
        if (bookInfo.getBookPrice() != null && bookInfo.getBookPrice().doubleValue() != 0) {
            c.andBetween("bookPrice", bookInfo.getBookPrice(), endPrice);
        }
        // 设置排序字段
        example.setOrderByClause("book_price desc");


        List<BookInfo> list = bookInfoMapper.selectByExample(example);

        return new PageInfo<>(list);
    }

    @Override
    public BookInfo findBookInfoByEdit(int id, String bookName) {
        Example example = new Example(BookInfo.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("bookName",bookName);
        c.andNotEqualTo("id", id);

        return bookInfoMapper.selectOneByExample(example);
    }
}
